package com.huohuzhihui.web.controller.api;

import com.huohuzhihui.api.service.ApiMerchantService;
import com.huohuzhihui.common.core.controller.BaseController;
import com.huohuzhihui.common.core.domain.AjaxResult;
import com.huohuzhihui.common.core.domain.entity.SysUser;
import com.huohuzhihui.common.core.domain.model.LoginUser;
import com.huohuzhihui.common.utils.DateUtils;
import com.huohuzhihui.douyin.domain.DouyinUserVideo;
import com.huohuzhihui.douyin.service.IDouyinUserVideoService;
import com.huohuzhihui.framework.web.service.SysLoginService;
import com.huohuzhihui.framework.web.service.TokenService;
import com.huohuzhihui.merchant.domain.MerCouponActivity;
import com.huohuzhihui.merchant.domain.MerCouponGet;
import com.huohuzhihui.merchant.domain.MerMerchant;
import com.huohuzhihui.merchant.service.IMerActivityBrowseService;
import com.huohuzhihui.merchant.service.IMerCouponActivityService;
import com.huohuzhihui.merchant.service.IMerCouponGetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 *微信api
 * 
 * @author lizhihe
 * @date 2020-11-22
 */
@RestController
@RequestMapping("/api")
public class ApiIndexController extends BaseController
{
    @Autowired
    private ApiMerchantService apiMerchantService;
    @Autowired
    private IMerActivityBrowseService merActivityBrowseService;
    @Autowired
    private IDouyinUserVideoService douyinUserVideoService;
    @Autowired
    private IMerCouponGetService merCouponGetService;
    @Autowired
    private IMerCouponActivityService merCouponActivityService;
    @Autowired
    private SysLoginService loginService;
    @Autowired
    private TokenService tokenService;

    @PostMapping("/loginByMerchantMobile")
    @ResponseBody
    public AjaxResult loginByMerchantMobile(@RequestBody MerMerchant merMerchant)
    {
        AjaxResult ajax = AjaxResult.success();
        String token = null;
        try {
            // 生成令牌
            token =   loginService.loginByMobile(merMerchant.getMobile(),merMerchant.getPassword());
            ajax.put("data", token);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return ajax;
    }

    /**
     * 商家首页获取今日视频转发总数
     * @param
     * @return
     */
    @GetMapping("/getTodayPushVideoCount")
    @ResponseBody
    public AjaxResult getTodayPushVideoCount(HttpServletRequest request)
    {
        AjaxResult ajax = AjaxResult.success();
        try {
            LoginUser loginUser =tokenService.getLoginUser(request);
            SysUser sysUser = loginUser.getUser();

            Date today = DateUtils.initDateByDay();//今日
            Date tomorrow = DateUtils.addDays(today,1);//明日

            DouyinUserVideo douyinUserVideo = new DouyinUserVideo();
            douyinUserVideo.setBeginTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",today));
            douyinUserVideo.setEndTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",tomorrow));
            douyinUserVideo.setMerchantId(sysUser.getDeptId());
            long todayVideos = douyinUserVideoService.selectDouyinUserVideoCount(douyinUserVideo);
            // 生成令牌
            ajax.put("data", todayVideos);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return ajax;
    }
    /**
     * 商家首页获取昨日视频转发总数
     * @param request
     * @return
     */
    @GetMapping("/getYestodayPushVideoCount")
    @ResponseBody
    public AjaxResult getYestodayPushVideoCount(HttpServletRequest request)
    {
        AjaxResult ajax = AjaxResult.success();
        try {
            LoginUser loginUser =tokenService.getLoginUser(request);
            SysUser sysUser = loginUser.getUser();

            Date today = DateUtils.initDateByDay();//今日
            Date yestoday = DateUtils.addDays(today,-1);//昨日

            DouyinUserVideo douyinUserVideo = new DouyinUserVideo();
            douyinUserVideo.setBeginTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",yestoday));
            douyinUserVideo.setEndTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",today));
            douyinUserVideo.setMerchantId(sysUser.getDeptId());
            long todayVideos = douyinUserVideoService.selectDouyinUserVideoCount(douyinUserVideo);
            // 生成令牌
            ajax.put("data", todayVideos);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return ajax;
    }
    /**
     * 商家首页获取今日视频转发总数
     * @param request
     * @return
     */
    @GetMapping("/getCouponGetCount")
    @ResponseBody
    public AjaxResult getCouponGetCount(HttpServletRequest request,String type)
    {
        AjaxResult ajax = AjaxResult.success();
        try {
            LoginUser loginUser =tokenService.getLoginUser(request);
            SysUser sysUser = loginUser.getUser();

            MerCouponGet merCouponGet = new MerCouponGet();
            merCouponGet.setMerchantId(sysUser.getDeptId());
            merCouponGet.setType(type);
            long todayCoupons = merCouponGetService.selectMerCouponGetCount(merCouponGet);
            ajax.put("data", todayCoupons);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return ajax;
    }


    /**
     * 商家首页获取今日核销优惠券总数
     * @param request
     * @return
     */
    @GetMapping("/getTodayCouponCheckCount")
    @ResponseBody
    public AjaxResult getTodayCouponCheckCount(HttpServletRequest request)
    {
        AjaxResult ajax = AjaxResult.success();
        try {
            LoginUser loginUser =tokenService.getLoginUser(request);
            SysUser sysUser = loginUser.getUser();

            Date today = DateUtils.initDateByDay();//今日
            Date tomorrow = DateUtils.addDays(today,1);//明日

            MerCouponGet merCouponGet = new MerCouponGet();
            merCouponGet.setBeginTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",today));
            merCouponGet.setEndTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",tomorrow));
            merCouponGet.setMerchantId(sysUser.getDeptId());
            merCouponGet.setUseStatus(1);
            long todayCoupons = merCouponGetService.selectMerCouponCheckCount(merCouponGet);
            ajax.put("data", todayCoupons);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return ajax;
    }

    /**
     * 商家首页获取昨日核销优惠券总数
     * @param request
     * @return
     */
    @GetMapping("/getYestodayCouponCheckCount")
    @ResponseBody
    public AjaxResult getYestodayCouponCheckCount(HttpServletRequest request)
    {
        AjaxResult ajax = AjaxResult.success();
        try {
            LoginUser loginUser =tokenService.getLoginUser(request);
            SysUser sysUser = loginUser.getUser();

            Date today = DateUtils.initDateByDay();//今日
            Date yestoday = DateUtils.addDays(today,-1);//昨日

            MerCouponGet merCouponGet = new MerCouponGet();
            merCouponGet.setBeginTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",yestoday));
            merCouponGet.setEndTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",today));
            merCouponGet.setMerchantId(sysUser.getDeptId());
            merCouponGet.setUseStatus(1);
            long todayCoupons = merCouponGetService.selectMerCouponCheckCount(merCouponGet);
            ajax.put("data", todayCoupons);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return ajax;
    }


    /**
     * 商家首页获取活动总数
     * @param request
     * @return
     */
    @GetMapping("/getActivityCount")
    @ResponseBody
    public AjaxResult getActivityCount(HttpServletRequest request,Long status)
    {
        AjaxResult ajax = AjaxResult.success();
        try {
            LoginUser loginUser =tokenService.getLoginUser(request);
            SysUser sysUser = loginUser.getUser();

            MerCouponActivity activity = new MerCouponActivity();
            activity.setMerchantId(sysUser.getDeptId());
            activity.setStatus(status);

           long count = this.merCouponActivityService.selectMerCouponActivityCount(activity);

            ajax.put("data", count);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return ajax;
    }

    /**
     * 查询登录用户信息
     * @param request
     * @return
     */
    @GetMapping("/getLoginUser")
    @ResponseBody
    public AjaxResult getLoginUser(HttpServletRequest request)
    {
        try {
            LoginUser loginUser =tokenService.getLoginUser(request);
            SysUser sysUser = loginUser.getUser();
            return AjaxResult.success("查询登录用户信息成功",sysUser);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }


}
